package client.comm.commlib.comm_ui

import android.Manifest
import android.app.Activity
import android.content.ContentResolver
import android.content.Intent
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.provider.OpenableColumns
import androidx.core.content.FileProvider
import androidx.core.net.toFile
import androidx.fragment.app.Fragment
import androidx.loader.app.LoaderManager
import client.comm.commlib.toast.toast
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions
import java.io.File
import kotlin.random.Random

class PhotoFragment : Fragment() {
    private val RC_CAMERA_PERM = 0x03
    private val REQUEST_CODE_CROP = 601
    private val REQUEST_CODE_CAMERA = 602
    private val REQUEST_CODE_SELECT = 603
    private var cropCallback: ((Uri?, Boolean, String) -> Unit)? = null
    private var selectCallback: ((Uri?, Boolean, String) -> Unit)? = null
    private var cameraCallback: ((Uri?, Boolean, String) -> Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setRetainInstance(true)
    }


    lateinit var cameraPath: String

    //调用相机
    fun camera(photoCallBack: ((Uri?, Boolean, String) -> Unit)?) {

        this.cameraCallback = photoCallBack

        if (!EasyPermissions.hasPermissions(requireContext(), Manifest.permission.CAMERA)) {
            EasyPermissions.requestPermissions(
                this,
                "请开启相机权限？",
                RC_CAMERA_PERM,
                Manifest.permission.CAMERA
            )
            return
        }
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1)
        val file = File(
            requireContext().externalCacheDir!!.absolutePath,
            "${System.currentTimeMillis()}${Random.nextInt(9999)}.jpg"
        )
        cameraPath = file.absolutePath
        val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION)
            FileProvider.getUriForFile(
                requireContext(),
                requireActivity().packageName + ".provider",
                file
            )
        } else {
            Uri.fromFile(file)
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
        startActivityForResult(intent, REQUEST_CODE_CAMERA)
    }

    //选择图片
    fun select(photoCallBack: (Uri?, Boolean, String) -> Unit) {
        this.selectCallback = photoCallBack
        val intent = Intent()
        intent.action = Intent.ACTION_PICK
        intent.type = "image/*"
        startActivityForResult(
            intent, REQUEST_CODE_SELECT
        )
    }

    var outUri: Uri? = null

    //裁剪
    fun crop(
        uri: Uri,
        aspectX: Int,
        aspectY: Int,
        outputX: Int,
        outputY: Int,
        callBack: (Uri?, Boolean, String) -> Unit
    ) {

        val uri1 = if (uri.scheme.equals(ContentResolver.SCHEME_FILE))
            FileProvider.getUriForFile(
                requireContext(),
                requireActivity().packageName + ".provider",
                uri.toFile()
            )
        else uri

        val cursor = requireContext().contentResolver.query(uri1, null, null, null, null, null)
        cursor?.let {
            it.moveToFirst()

            this.cropCallback = callBack
            val intent = Intent("com.android.camera.action.CROP")
            //文件名
            val displayName = it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
            outUri = Uri.fromFile(
                File(
                    requireContext().externalCacheDir!!.absolutePath,
                    "crop${Random.nextInt(0, 9999)}$displayName"
                )
            )
            requireContext().grantUriPermission(
                requireContext().getPackageName(),
                outUri,
                Intent.FLAG_GRANT_READ_URI_PERMISSION
            )
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.putExtra("noFaceDetection", true) //去除默认的人脸识别，否则和剪裁匡重叠
            intent.setDataAndType(uri1, requireContext().contentResolver.getType(uri))
            intent.putExtra("crop", "true") // crop=true 有这句才能出来最后的裁剪页面.
            intent.putExtra("output", outUri)
            intent.putExtra("outputFormat", "JPEG") // 返回格式
            var ax = aspectX
            var ay = aspectY
            if (ay != 0 && ay != 0) {
                if (ax == ay && Build.MANUFACTURER == "HUAWEI") {
                    ax = 9998
                    ay = 9999
                }
                intent.putExtra("aspectX", ax) // 这两项为裁剪框的比例.
                intent.putExtra("aspectY", ay) // x:y=1:2
            }
            if (outputX != 0 && outputY != 0) {
                intent.putExtra("outputX", outputX)
                intent.putExtra("outputY", outputY)
            }
            intent.putExtra("return-data", false)
            startActivityForResult(
                intent, REQUEST_CODE_CROP
            )
        }
        cursor?.close()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                REQUEST_CODE_CROP -> {
                    //裁剪
                    //把outUri转成
                    val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        //把uri转成匿名uri
                        if (outUri?.scheme.equals(ContentResolver.SCHEME_FILE))
                            FileProvider.getUriForFile(
                                requireContext(),
                                requireActivity().packageName + ".provider", outUri!!.toFile()
                            )
                        else outUri
                    } else outUri
                    cropCallback?.invoke(uri, true, "")
                    cropCallback = null
                }
                REQUEST_CODE_CAMERA -> {
                    val uri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        FileProvider.getUriForFile(
                            requireContext(),
                            requireActivity().packageName + ".provider", File(cameraPath)
                        )
                    } else {
                        Uri.fromFile(File(cameraPath))
                    }
                    cameraCallback?.invoke(uri, true, "")
                    cameraCallback = null
                }
                REQUEST_CODE_SELECT -> {
                    val uri = data?.data
                    selectCallback?.invoke(uri, true, "")
                    selectCallback = null
                }
                else -> {

                }
            }
        } else {
            when (requestCode) {
                REQUEST_CODE_CROP -> {
                    //裁剪
                    cropCallback?.invoke(null, false, "裁剪失败")
                    cropCallback = null
                }
                REQUEST_CODE_CAMERA -> {

                    cameraCallback?.invoke(null, false, "拍照失败")
                    cameraCallback = null
                }
                REQUEST_CODE_SELECT -> {
                    selectCallback?.invoke(null, false, "选择图片失败")
                    selectCallback = null
                }
            }
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        when (requestCode) {
            RC_CAMERA_PERM -> {
                if (EasyPermissions.hasPermissions(requireContext(), Manifest.permission.CAMERA)) {
                    camera(this.cameraCallback)
                } else {
                    toast("权限被拒绝")
                }
            }
        }
    }

}